<?php
/**
 * CreateTime: 2024/1/16 14:38
 * User:LinQ
 */

namespace App\Http\Service\Goods;

use App\Enum\GoodsEnum;
use App\Http\Service\BasicInterfaceService;
use App\Models\Goods\GoodsModel;

class GoodsService implements BasicInterfaceService
{

    private function listSelectDefaultFilters($goodsModel)
    {
        $goodsModel->pushFilter('status',1);
    }

    public function getList(array $request)
    {
        $page_size = $prams['page_size'] ?? 10;
        $filters = $prams['filters'] ?? [];
        $desc = $prams['desc'] ?? [];
        $goodsModel = new GoodsModel();
        $this->listSelectDefaultFilters($goodsModel);
        if($filters){
            $this->listSelectFilters($goodsModel,$filters);
        }
        $res = $goodsModel->cListHasCom($goodsModel,$page_size,$desc,[
            'goods_show_image_id',
            'goods_image_ids',
            'goods_name',
            'goods_simple_name',
            'com_id as com_str'
        ]);
//        dd($res['data']->goods_name);
        // 处理数据
        $res['data'] = $this->dispose($res['data']);
        s_c_return($res,trans('message.get_list_ok',[
            'name' => trans('key.goods')
        ]));
    }

    public function dispose($data): mixed
    {
        return $data;
    }

    public function listSelectFilters($query, $filters)
    {
        if(map_has_value($filters,'goods_name')){
            $query->pushFilter('goods_name',$filters['goods_name'],'like');
        }
        if(map_has_value($filters,'group_id')){
            $query->pushFilter('group_id',$filters['group_id']);
        }
        if(map_has_value($filters,'goods_type_id')){
            $query->pushFilter('goods_type_id',$filters['goods_type_id']);
        }
        if(map_has_value($filters,'stock_status')){
            $query->pushFilter('stock_status',$filters['stock_status']);
        }
        if(map_has_value($filters,'sales_method')){
            $query->pushFilter('sales_method',$filters['sales_method']);
        }
    }

    public function getSelect()
    {

    }

    public function getSelectHasPage($prams)
    {

    }

    public function getInfo($id)
    {

    }

    public function create($data)
    {

    }

    public function fakeDelete($id)
    {

    }

    public function fakeDeleteByIds($ids)
    {

    }

    public function delete($id)
    {

    }

    public function update($data)
    {

    }

    public function getGoodsListV1(array $request)
    {
        $page_size = $prams['page_size'] ?? 10;
        $filters = $prams['filters'] ?? [];
        $desc = $prams['desc'] ?? [];
        $goodsModel = new GoodsModel();
        $this->listSelectDefaultFilters($goodsModel);
        // 规定类型为单领
        $goodsModel->pushFilter('sales_method',GoodsEnum::SALES_METHOD_1);
        if($filters){
            $this->listSelectFilters($goodsModel,$filters);
        }
        $res = $goodsModel->cListHasCom($goodsModel,$page_size,$desc,[
            'goods_show_image_id',
            'goods_image_ids',
            'goods_name',
            'goods_simple_name',
            'com_id as com_str'
        ]);
//        dd($res['data']->goods_name);
        // 处理数据
        $res['data'] = $this->dispose($res['data']);
        s_c_return($res,trans('message.get_list_ok',[
            'name' => trans('key.goods')
        ]));
    }

    public function getGoodsListV2(array $request)
    {
        $page_size = $prams['page_size'] ?? 10;
        $filters = $prams['filters'] ?? [];
        $desc = $prams['desc'] ?? [];
        $goodsModel = new GoodsModel();
        // 规定类型为拼团
        $goodsModel->pushFilter('sales_method',GoodsEnum::SALES_METHOD_1);
        $this->listSelectDefaultFilters($goodsModel);
        if($filters){
            $this->listSelectFilters($goodsModel,$filters);
        }
        $res = $goodsModel->cListHasCom($goodsModel,$page_size,$desc,[
            'goods_show_image_id',
            'goods_image_ids',
            'goods_name',
            'goods_simple_name',
            'com_id as com_str'
        ]);
//        dd($res['data']->goods_name);
        // 处理数据
        $res['data'] = $this->dispose($res['data']);
        s_c_return($res,trans('message.get_list_ok',[
            'name' => trans('key.goods')
        ]));
    }
}
